package com.pzhyd.sc.biz.impl;

import java.util.ArrayList;
import java.util.List;

import com.pzhyd.sc.biz.inter.IuserBiz;
import com.pzhyd.sc.dao.inter.IBaseDao;
import com.pzhyd.sc.entity.UsersInfo;
import com.pzhyd.sc.util.Page;
/** 
* @Project: webvote
* @Title: UserBizImpl.java
* @Package com.pzhyd.sc.biz.impl
* @Description: 用户信息逻辑层操作
* @author Gaoshiwei 812257531@qq.com
* @date Jul 9, 2012 2:34:14 PM
* @Copyright: 2012 攀枝花移动 All rights reserved.
* @version V1.0  
 */
public class UserBizImpl implements IuserBiz {
	
	private IBaseDao ibd;//数据dao层，负责与数据库进行交互
	
	public IBaseDao getIbd() {
		return ibd;
	}

	public void setIbd(IBaseDao ibd) {
		this.ibd = ibd;
	}

	public boolean deleteUserInfo(UsersInfo user) throws Exception {
		// TODO Auto-generated method stub
		int count = 0;
		boolean flag=false;
		try {
			count = ibd.delete(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(count>0){
			flag=true;
		}else{
			flag=false;
		}
		return flag;
	}

	public UsersInfo queryPassbyUsersName(String usersName) throws Exception {
		// TODO Auto-generated method stub
		String hql = "from UsersInfo u where u.usersName='"+usersName+"'";
		List<UsersInfo> lst = null;
		try {
			lst = ibd.searchHql(hql, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		UsersInfo ui=lst.get(0);
		return ui;
	}

	public void queryUserInfo(Page page)
			throws Exception {
		// TODO Auto-generated method stub
		String hql1 = "select count(*) from UsersInfo";
		String hql = "from UsersInfo ui order by ui.usersId ";
		try {
			page.setTotalRecords(ibd.getTotalCount(hql1, null));
			ibd.searchHql(hql, null, page);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public UsersInfo queryUserInfobyusersId(int usersId) throws Exception {
		// TODO Auto-generated method stub
		UsersInfo ui=null;
		try {
			ui=(UsersInfo) ibd.searchById(UsersInfo.class, usersId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ui;
	}

	public boolean queryUserbyusersAcount(String usersAcount) throws Exception {
		// TODO Auto-generated method stub
		boolean flag=false;
		String hql = "from UsersInfo where usersAccount='"+usersAcount+"'";
		List lst=null;
		 try {
			lst=ibd.searchHql(hql, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("打印~~~"+lst);
		for(int i=0;i<lst.size();i++){
			System.out.println(lst.get(i));
		}
		if(!(lst+"").equals("[]")){
			flag=true;
		}
		return flag;
	}

	public boolean queryisUserbyusersName(String usersName) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean queryuserRolbyusersId(int usersId) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean updatePass(String usersAcount, String pass) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	public int updateUserInfo(UsersInfo user) throws Exception {
		// TODO Auto-generated method stub
		int count = 0;
		try {
			count = ibd.update(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	
	//用户登录
	public UsersInfo userLogin(String account, String pwd) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("验证用户登录...");
		UsersInfo user = null;
		String hql = "from UsersInfo as u where u.usersAccount = ? and u.usersPass=?";
		List param = new ArrayList();
		param.add(account);
		param.add(pwd);		
		List list = this.ibd.searchHql(hql, param);		
		if(list.size()>0 && list!=null){
			user = (UsersInfo)list.get(0);
		}
		return user;
	}

	public boolean insertUserInfo(UsersInfo user)  {
		// TODO Auto-generated method stub
		int count = 0;
		boolean flag=false;
		count = ibd.insert(user);
		if(count>0){
			flag=true;
		}else{
			flag=false;
		}
		return flag;
	}

	public int deleteUsersByUids(int[] uid) throws Exception {
		// TODO Auto-generated method stub
		int count=0;
		StringBuilder sbStr = new StringBuilder();
		for (int i=0; i<uid.length; i++) {
			sbStr.append("?");
			if (i < (uid.length - 1)) {
				sbStr.append(",");
			}
		}
		List params = new ArrayList();
		for (int i=0; i<uid.length; i++) {
			params.add(uid[i]);
		}
		
		 String hql = "delete from UsersInfo where usersId in (" + sbStr.toString()  +  ")";
		 try {
			count=ibd.operateHql(hql, params);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return count;
	}

	public UsersInfo queryUserInfobyareaId(String aid) throws Exception {
		// TODO Auto-generated method stub
		String hql = "from UsersInfo u where u.areaInfo.aid='"+aid+"'";
		List<UsersInfo> lst = null;
		try {
			lst = ibd.searchHql(hql, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		UsersInfo ui=lst.get(0);
		return ui;
	}

	public List<UsersInfo> searchAlluser() throws Exception {
		// TODO Auto-generated method stub
		String hql = "from UsersInfo u order by u.usersId";
		List<UsersInfo> lst = null;
		try {
			lst = ibd.searchHql(hql, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lst;
	}
}